<?php

class PdoNews extends PdoConnect {

    public function create(news $news) {
        try {
            $this->open();
            $sql = "INSERT INTO `news`(TYPENEWS, DATENEWS, CONTENUNEWS, TITRENEWS, IDADMIN, IDNEWS) VALUES(?, ?, ?, ?, ?, ?)";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $news->getTypeNews(), PDO::PARAM_STR);
            $prep->bindValue(2, $news->getDateNews(), PDO::PARAM_STR);
            $prep->bindValue(3, $news->getContenuNews(), PDO::PARAM_STR);
            $prep->bindValue(4, $news->getTitreNews(), PDO::PARAM_STR);
            $prep->bindValue(5, $news->getIdAdmin(), PDO::PARAM_INT);
            $prep->bindValue(6, $news->getIdNews(), PDO::PARAM_INT);

            $prep->execute();
            $this->close();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function readAll() {
        try {
            $this->open();
            $sql = "SELECT * From `news`";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();
            $prep->setFetchMode(PDO::FETCH_ASSOC);


            return $prep;
            $this->close();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function readOne($IdNews) {
        try {
            $this->open();
            $sql = "SELECT * From `news` Where IdNews=$IdNews";
            $prep = $this->pdo->prepare($sql);

            $prep->execute();

            $news = new News();
            $ligne = $prep->fetch(PDO::FETCH_OBJ);
            $news->setIdNews($ligne->IDNEWS);
            $news->setTypeNews($ligne->TYPENEWS);
            $news->setDateNews($ligne->DATENEWS);
            $news->setContenuNews($ligne->CONTENUNEWS);
            $news->setTitreNews($ligne->TITRENEWS);
            $news->setIdAdmin($ligne->IDADMIN);
            return $news;
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function delete(news $news) {

        try {
            $this->open();
            $sql = "DELETE FROM news WHERE IDNEWS=?";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $news->getIdNews(), PDO::PARAM_INT);
            $prep->execute();
            $this->close();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function update(news $news) {
        try {
            $this->open();
            $sql = "UPDATE news SET TITRENEWS = ?, CONTENUNEWS= ?, TYPENEWS= ?, IDADMIN=?, DATENEWS=? WHERE IDNEWS=?";

            $prep = $this->pdo->prepare($sql);

            $prep->bindValue(1, $news->getTitreNews(), PDO::PARAM_STR);
            $prep->bindValue(2, $news->getContenuNews(), PDO::PARAM_STR);
            $prep->bindValue(3, $news->getTypeNews(), PDO::PARAM_STR);
            $prep->bindValue(4, $news->getIdAdmin(), PDO::PARAM_INT);
            $prep->bindValue(5, $news->getDateNews(), PDO::PARAM_STR);
            $prep->bindValue(6, $news->getIdNews(), PDO::PARAM_INT);

            $prep->execute();
            $this->close();
        } catch (PDOException $e) {
            $msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
            die($msg);
        }
    }

    public function exist($TITRENEWS) {
        $this->open();
        $query = "SELECT * FROM news WHERE TITRENEWS='$TITRENEWS';";
        $statement = $this->pdo->query($query);
        return $statement->rowCount();
        $this->close();
    }

}
